package de.dtools.n2g.core;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.*;

import javax.swing.*;

import com.lowagie.text.Document;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class PdfPrinter {

	int inch = Toolkit.getDefaultToolkit().getScreenResolution();
	float pixelToPoint = (float) 72 / (float) inch;
	
	public PdfPrinter(){
		super();
	}

	public void paintToPDF(JTextPane ta, File file) {
		try {
//			System.out.println("inch: " + inch + " ; p2p: " + pixelToPoint);
//			ta.setBounds(0, 0, (int) convertToPixels(612 - 58),
//					(int) convertToPixels(792 - 60));

			Document document = new Document();
			FileOutputStream fos = new FileOutputStream(file);
			PdfWriter writer = PdfWriter.getInstance(document, fos);
			
			
			int len = Integer.parseInt(ta.getDocument().getEndPosition().toString());
			
//			ta.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));
			document.setPageSize(new com.lowagie.text.Rectangle(612, 792));
			document.setHeader(new HeaderFooter(new Phrase("Titel des Dokumentes"), false));
			document.setFooter(new HeaderFooter(new Phrase("Note2Go\t\t\t"), false));
			document.open();
			PdfContentByte cb = writer.getDirectContent();

//			cb.saveState();
//			Graphics2D g2 = cb.createGraphics(, 792, null, true, .95f);

			AffineTransform at = new AffineTransform();
			at.translate(convertToPixels(20), convertToPixels(20));
			at.scale(pixelToPoint, pixelToPoint);

			Rectangle alloc = getVisibleEditorRect(ta);

            float pWidth = (float) 612;
            float pHeight = (float) 792; 
            
            PdfTemplate tp;
            Graphics2D g2;
            // Assuming that the number of pages would be 3, You can calculate this same way it is calculated
            // in print (Graphics g, PageFormat pageFormat, int pageIndex) method
            int numberOfPages = (int)Math.ceil(len/pHeight);
            
            System.out.println("numberOfPages: " + numberOfPages + " ; len: " + len);
            for (int i = 0; i < numberOfPages; i++) {
                tp = cb.createTemplate((float) 1.1 * pWidth, (float) 1.1 * pHeight);

                cb.concatCTM(1, 0, 0, 1, 0, 0);
                g2 = tp.createGraphicsShapes(pWidth, pHeight);
                g2.translate(0f, 30);
                g2.translate(0f, -i * pHeight * pixelToPoint);
                g2.translate(0f, 30);
//                g2.translate(convertToPixels(20), convertToPixels(20));
                g2.scale(pixelToPoint, pixelToPoint);
//                g2.transform(at);
                g2.setClip(0, (int)pHeight * i, (int)pWidth, (int)pHeight);
                ta.getUI().getRootView(ta).paint(g2, alloc);
                g2.dispose();
                cb.addTemplate(tp, 0, 0);
                document.newPage();
            }
//			cb.restoreState();
			document.close();
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public float convertToPoints(int pixels) {
		return (float) (pixels * pixelToPoint);
	}

	public float convertToPixels(int points) {
		return (float) (points / pixelToPoint);
	}

	protected Rectangle getVisibleEditorRect(JTextPane ta) {
		Rectangle alloc = ta.getBounds();
		if ((alloc.width > 0) && (alloc.height > 0)) {
			alloc.x = alloc.y = 0;
			Insets insets = ta.getInsets();
			alloc.x += insets.left;
			alloc.y += insets.top;
			alloc.width -= insets.left + insets.right;
			alloc.height -= insets.top + insets.bottom;
			return alloc;
		}
		return null;
	}
	
	

}
